(commands ls)
;; TODO: use previous result
(define ls
  (lambda* (#:key
            (location (receive (result _) (pwd) (car result)))
            (show-hidden? #f)
            ;; command interface
            (previous-result '())
            (shell-state default-shell-state))
    "List files in the given LOCATION. If LOCATION is not specified, then
list files in the current working directory. Return a list of file
objects."
    (let ([result
           (let ([fs-tree (file-system-tree location)])
             (match fs-tree
               ;; directory
               [(name stat children ...)
                (let ([first-level-children
                       ;; filter out hidden files if they are not wanted
                       (filter (λ (child)
                                 (or show-hidden?
                                     (and (not show-hidden?)
                                          (not (hidden? child)))))
                               (flat-children children))])
                  (for-each (λ (child)
                              (match child
                                [(name stat)
                                 (simple-format #t "~a\n" name)]))
                            first-level-children)
                  first-level-children)]
               ;; flat file
               [(name stat)
                (simple-format #t "~a\n" stat)]))])
      (values (list result)
              shell-state))))


(alias list-dir ls)
